//
//  InformationVC.m
//  echo
//
//  Created by dllo on 16/2/26.
//  Copyright © 2016年 Danny. All rights reserved.
//

#define WIDTH [UIScreen mainScreen].bounds.size.width / 414
#define HEIGHT [UIScreen mainScreen].bounds.size.height / 736

#import "InformationVC.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "CellForInformationVCSectionZero.h"
#import "CellForInformationVCSectionOneKindOne.h"
#import "CellForInformationVCSectionOneKindTwo.h"
#import "CellForInformationVCSectionOneKindThree.h"
#import <DKNightVersion.h>
#import "NetworkingHandler.h"
#import "ModelForInformationVC.h"
#import "CellForInformationVCSectionOneKindOne.h"
#import "CellForInformationVCSectionOneKindTwo.h"
#import "CellForInformationVCSectionOneKindThree.h"
#import "CellForInformationVCSectionOneKindFour.h"
#import "ModelForInformationVCSectionOneKindOne.h"
#import "ModelForInformationVCSectionOneKindTwo.h"
#import "ModelForInformationVCSectionOneKindThree.h"
#import "ModelForInformationVCSectionOneKindFour.h"
#import <Social/Social.h>

@interface InformationVC ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate, CellForInformationVCSectionZero>
@property (nonatomic, retain)UITableView *tableView;
//@property (nonatomic, retain)UIImageView *imageViewForHead;
@property (nonatomic, retain)UIVisualEffectView *visualView;
@property (nonatomic, retain)UIImageView *imageOfPic;
@property (nonatomic, retain)UIButton *buttonForBack;
@property (nonatomic, retain)NSMutableArray *arrForData;
@property (nonatomic, retain)NSMutableArray *arrForCount;
@property (nonatomic, retain)UILabel *labelForName;
@property (nonatomic, retain)NSMutableArray *arrForSectionOne;
@end

@implementation InformationVC

- (void)dealloc{
//    [_imageViewForHead release];
    [_visualView release];
    [_imageOfPic release];
    [_buttonForBack release];
    [_arrForData release];
    [_id release];
    [_arrForCount release];
    [_labelForName release];
    [_arrForSectionOne release];
    self.tableView.delegate = nil;
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    [self handleDataForInformationCell];
}

- (void)handleDataForInformationCell{
    
    NSString *str = @"http://echosystem.kibey.com/user/info?user_id=";
    NSString *str2 = @"http://echosystem.kibey.com/feed/mine?user_id=";
    NSString *url = [str stringByAppendingString:self.id];
    NSString *url2 = [str2 stringByAppendingFormat:@"%@&page=1", self.id];
    [NetworkingHandler networkingGetWithUrl:url dicForValue:@{@"x-v":@"20160209",@"x-c":@"3",@"Cookie":@"PHPSESSID=e738h8vpavbcj8efq2prkhond4"} completeHandle:^(id result) {
        NSDictionary *dic = [result objectForKey:@"result"];
        NSDictionary *dic2 = [dic objectForKey:@"user"];
        NSDictionary *dic3 = [dic objectForKey:@"statistics"];
        self.arrForData = [NSMutableArray array];
        ModelForInformationVC *model = [[ModelForInformationVC alloc]init];
        [model setValuesForKeysWithDictionary:dic2];
        [model setValuesForKeysWithDictionary:dic3];
        [self.arrForData addObject:model];
    }];

    [NetworkingHandler networkingGetWithUrl:url2 dicForValue:@{@"x-v":@"20160209",@"x-c":@"3",@"Cookie":@"PHPSESSID=mip82ccuo4em5a8mtl9vifh3j0"} completeHandle:^(id result) {
        self.arrForCount = [NSMutableArray array];
        self.arrForCount = [result objectForKey:@"result"];
        self.arrForSectionOne = [NSMutableArray array];
        for (NSDictionary *dic in self.arrForCount) {
            
            if ([dic objectForKey:@"mv"]) {
                NSDictionary *dicForMv = [dic objectForKey:@"mv"];
                ModelForInformationVCSectionOneKindOne *model = [[ModelForInformationVCSectionOneKindOne alloc]init];
                [model setValuesForKeysWithDictionary:dicForMv];
                [self.arrForSectionOne addObject:model];
            }else if ([dic objectForKey:@"sound"]){
                ModelForInformationVCSectionOneKindTwo *model = [[ModelForInformationVCSectionOneKindTwo alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                NSDictionary *dicForSound = [dic objectForKey:@"sound"];
                [model setValuesForKeysWithDictionary:dicForSound];
                [self.arrForSectionOne addObject:model];
            }else if ([dic objectForKey:@"image"]){
                ModelForInformationVCSectionOneKindThree *model = [[ModelForInformationVCSectionOneKindThree alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                NSArray *arrForImage = [dic objectForKey:@"image"];
                NSDictionary *tempDic = [arrForImage objectAtIndex:0];
                [model setValuesForKeysWithDictionary:tempDic];
                [self.arrForSectionOne addObject:model];
              
            }else{
                ModelForInformationVCSectionOneKindFour *model = [[ModelForInformationVCSectionOneKindFour alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                NSDictionary *dicForFollow = [dic objectForKey:@"follow_user"];
                [model setValuesForKeysWithDictionary:dicForFollow];
                [self.arrForSectionOne addObject:model];
            }
        }
        [self createTableView];
    }];
}

- (void)handleBackForInformation{
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)createTableView{
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -135 * HEIGHT, self.view.frame.size.width, self.view.frame.size.height + 135 * HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
    [self.view addSubview:self.tableView];
    [self.tableView release];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.bounces = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.tableView registerClass:[CellForInformationVCSectionZero class] forCellReuseIdentifier:@"poolForInfomationVCSectionOneKindOne"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"poolForSectionZeroRowZero"];
    [self.tableView registerClass:[CellForInformationVCSectionOneKindOne class] forCellReuseIdentifier:@"poolForInformationVCSectionOneKindOne"];
    [self.tableView registerClass:[CellForInformationVCSectionOneKindTwo class] forCellReuseIdentifier:@"poolForInformationVCSectionOneKindTwo"];
    [self.tableView registerClass:[CellForInformationVCSectionOneKindThree class] forCellReuseIdentifier:@"poolForInformationVCSectionOneKindThree"];
    [self.tableView registerClass:[CellForInformationVCSectionOneKindFour class] forCellReuseIdentifier:@"poolForInformationVCSectionOneKindFour"];
    
    
    ModelForInformationVC *model = [self.arrForData objectAtIndex:0];
    if ([UIScreen mainScreen].bounds.size.height == 480) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 142 * HEIGHT)];

        if ([[model.avatar substringToIndex:1] isEqualToString:@"/"]) {
            NSString *str1 = @"http://kibey-sys-avatar.b0.upaiyun.com";
            NSString *strForPic = [str1 stringByAppendingString:model.avatar];
            [imageView sd_setImageWithURL:[NSURL URLWithString:strForPic]];
        }else{
            [imageView sd_setImageWithURL:[NSURL URLWithString:model.avatar]];
            
        }
        
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.tag = 101;
        imageView.userInteractionEnabled = YES;
        
        [self.tableView addSubview:imageView];
        UIBlurEffect *blur = [UIBlurEffect effectWithStyle:0];
        self.visualView = [[UIVisualEffectView alloc]initWithEffect:blur];
        
        [self.tableView addSubview:self.visualView];
        
        self.visualView.frame = CGRectMake(0, 64 * HEIGHT, [UIScreen mainScreen].bounds.size.width, 253 * HEIGHT);
        self.visualView.alpha = 0.8;
        
        [_visualView release];
    }else{
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200 * HEIGHT)];

        if ([[model.avatar substringToIndex:1] isEqualToString:@"/"]) {
            NSString *str1 = @"http://kibey-sys-avatar.b0.upaiyun.com";
            NSString *strForPic = [str1 stringByAppendingString:model.avatar];
            [imageView sd_setImageWithURL:[NSURL URLWithString:strForPic]];
        }else{
            [imageView sd_setImageWithURL:[NSURL URLWithString:model.avatar]];
            
        }
        
        
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.tag = 101;
        imageView.userInteractionEnabled = YES;
        
        [self.tableView addSubview:imageView];
        UIBlurEffect *blur = [UIBlurEffect effectWithStyle:0];
        self.visualView = [[UIVisualEffectView alloc]initWithEffect:blur];
        
        [self.tableView addSubview:self.visualView];
        
        self.visualView.frame = CGRectMake(0, 64 * HEIGHT, [UIScreen mainScreen].bounds.size.width, 245 * HEIGHT);
        self.visualView.alpha = 0.8;
        
        [_visualView release];

    }
    
    
    self.imageOfPic = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 50 * WIDTH, self.visualView.bounds.size.height - 120 * HEIGHT, 100 * WIDTH, 100 * WIDTH)];
    self.imageOfPic.layer.cornerRadius = 50 * WIDTH;
    self.imageOfPic.layer.masksToBounds = YES;

    if ([[model.avatar substringToIndex:1] isEqualToString:@"/"]) {
        NSString *str1 = @"http://kibey-sys-avatar.b0.upaiyun.com";
        NSString *strForPic = [str1 stringByAppendingString:model.avatar];
     [self.imageOfPic sd_setImageWithURL:[NSURL URLWithString:strForPic]];
    }else{
        [self.imageOfPic sd_setImageWithURL:[NSURL URLWithString:model.avatar]];
    }
    
    [self.visualView addSubview:self.imageOfPic];
    [self.imageOfPic release];
    
    self.buttonForBack = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonForBack.frame = CGRectMake(7 * WIDTH, 25 * HEIGHT, 40 * WIDTH, 35 * HEIGHT);
    [self.view addSubview:self.buttonForBack];
    [self.buttonForBack setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    self.navigationItem.hidesBackButton = YES;
    [self.buttonForBack addTarget:self action:@selector(handleBackForInformation) forControlEvents:UIControlEventTouchUpInside];
    
    self.labelForName = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 41 * WIDTH, 25 * HEIGHT, 80 * WIDTH, 25 * HEIGHT)];
    [self.view addSubview:self.labelForName];
    self.labelForName.text = model.name;
    self.labelForName.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
    self.labelForName.textAlignment = 1;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 175 * HEIGHT;
        
    }else{
        return 40 * HEIGHT;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 88 * HEIGHT;
        }else{
            CellForInformationVCSectionZero *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForInfomationVCSectionOneKindOne"];
            ModelForInformationVC *model = [self.arrForData objectAtIndex:0];
            return [cell heightForCell:model];
        }
    }else{
        NSDictionary *dic = [self.arrForCount objectAtIndex:indexPath.row];
        if ([dic objectForKey:@"mv"]) {
            return 282 * HEIGHT;
        }else if ([dic objectForKey:@"sound"]){
            return 275 * HEIGHT;
        }else if ([dic objectForKey:@"image"]){
            
            return 310 * HEIGHT;
        }else{
            return 140;
        }
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return nil;
    }else{
        UIView *viewOfBack = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40 * HEIGHT)];
        viewOfBack.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:218 / 255.f green:219 / 255.f blue:216 / 255.f alpha:1], [UIColor colorWithRed:17 / 256.f green:27 / 256.f blue:47 / 256.f alpha:1]);
        UILabel *labelForSectionOne = [[UILabel alloc]initWithFrame:CGRectMake(10 * WIDTH, 5 * HEIGHT, self.view.frame.size.width - 20 * WIDTH, 40 * HEIGHT)];
        [viewOfBack addSubview:labelForSectionOne];
        [labelForSectionOne release];
        NSString *str = @"      动态";
        labelForSectionOne.text = [str stringByAppendingFormat:@"(%ld)", (unsigned long)self.arrForCount.count];
        labelForSectionOne.font = [UIFont fontWithName:@"Arial" size:17.0];
        labelForSectionOne.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor whiteColor]);
        return viewOfBack;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 2;
    }else{
        return self.arrForCount.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {

            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForSectionZeroRowZero"];
            cell.backgroundColor = [UIColor blackColor];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

            return cell;
        }else{
            
            CellForInformationVCSectionZero *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForInfomationVCSectionOneKindOne"];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            ModelForInformationVC *model = [self.arrForData objectAtIndex:0];
            [cell passModelForInformationCell:model];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            cell.delegate = self;
            return cell;
        }
        
    }else{
        
        id model = [self.arrForSectionOne objectAtIndex:indexPath.row];
        if ([[model class] isSubclassOfClass:[ModelForInformationVCSectionOneKindOne class]]) {
            ModelForInformationVCSectionOneKindOne *model = [self.arrForSectionOne objectAtIndex:indexPath.row];
            CellForInformationVCSectionOneKindOne *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForInformationVCSectionOneKindOne"];
            [cell passModel:model];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            return cell;
        }else if ([[model class] isSubclassOfClass:[ModelForInformationVCSectionOneKindTwo class]]){
            ModelForInformationVCSectionOneKindTwo *model = [self.arrForSectionOne objectAtIndex:indexPath.row];
            CellForInformationVCSectionOneKindTwo *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForInformationVCSectionOneKindTwo"];
            [cell passModel:model];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            return cell;
        }else if ([[model class] isSubclassOfClass:[ModelForInformationVCSectionOneKindThree class]]){
            ModelForInformationVCSectionOneKindThree *model = [self.arrForSectionOne objectAtIndex:indexPath.row];
            CellForInformationVCSectionOneKindThree *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForInformationVCSectionOneKindThree"];
            [cell passModel:model];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            return cell;
        }else{
            ModelForInformationVCSectionOneKindFour *model = [self.arrForSectionOne objectAtIndex:indexPath.row];
            CellForInformationVCSectionOneKindFour *cell = [tableView dequeueReusableCellWithIdentifier:@"poolForInformationVCSectionOneKindFour"];
            [cell passModel:model];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            return cell;
        }
        return nil;
    }
}

- (void)presentComposeVc:(SLComposeViewController *)composeVc{
    [self presentViewController:composeVc animated:YES completion:nil];
}

//- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    
//    CGPoint point = scrollView.contentOffset;
//    if (point.y < - 0) {
//        CGRect rect = [self.tableView viewWithTag:101].frame;
//        rect.origin.y = point.y;
//        rect.size.height = -point.y;
////        [self.tableView viewWithTag:101].frame = rect;
//        
//        CGRect rect2 = self.visualView.frame;
//        rect2.origin.y = point.y;
//        rect2.size.height = -point.y;
////        self.visualView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 150 *HEIGHT -point.y);
//        
////        self.visualView.frame = CGRectMake(0, -64, [UIScreen mainScreen].bounds.size.width, 245 * HEIGHT - (scrollView.contentOffset.y)/2);
//        
//    }
//    
//}

- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
        
        [DKNightVersionManager nightFalling];
        
    } else {
        
        [DKNightVersionManager dawnComing];
    }

    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
